//
//  MHCHChoisCityViewController.m
//  Go!
//
//  Created by rimi on 16/7/11.
//  Copyright © 2016年 MHCH. All rights reserved.
//

#import "MHCHChoisCityViewController.h"
#import "Tools.h"

#import "MHCHCitysModel.h"



@interface MHCHChoisCityViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (nonatomic, strong) UILabel_Font *currentCityNameLable; /**< 显示当前城市的lable */
@property (nonatomic, strong) UIPickerView *choisCityPickerView; /**< 城市选择器 */
@property (nonatomic, strong) NSString *provinceName; /**< 选择县 */
@property (nonatomic, strong) NSString *cityName;  /**< 选择城市 */
@property (nonatomic, strong) NSString *regionName;  /**< 选择区域 */
@property (nonatomic, strong) NSArray *citiesData;  /**< 数据对象组 */
@property (nonatomic, strong) UILabel_Font *choisCityLable;  /**< 选择好了的城市 */
@property (nonatomic, strong) UIButton *sureBtn;  /**< 确定按钮 */
@property (nonatomic, strong) UIButton *cacelBtn;  /**< 取消按钮 */
@property (nonatomic, strong) NSArray *citymessage;  /**< 城市信息 */



@end

@implementation MHCHChoisCityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = ColorWith(255, 255, 255);
    self.navigationItem.title = @"城市";
    [self initializeDataSource];
    [self initializeUserInterface];
}

-(void)initializeDataSource/**< 初始化数据源 */{
    
    self.citymessage = [NSArray arrayWithArray:[MHCHCitysModel getTheCityMessage]];
    
}
-(void)initializeUserInterface/**< 初始化用户界面 */{
    [self.view addSubview:self.choisCityPickerView];
    [self.view addSubview:self.choisCityLable];
    [self.view addSubview:self.sureBtn];
    [self.view addSubview:self.cacelBtn];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    

}

#pragma mark - UIPickerViewDataSource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
         // 返回城市数量
    if (self.citymessage && self.citymessage.count) {
        return self.citymessage.count;
    }else{
        return 1;
    }
}

#pragma mark - UIPickerViewDelegate

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    switch (component) {
        case 0:
            return [NSString stringWithString:self.citymessage[row][@"cityname"]];
            break;
        default:
            return nil;
            break;
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

    switch (component) {
        case 0:
            if (self.citymessage) {
                self.cityName = self.citymessage[row][@"cityname"];
                self.choisCityLable.text = self.cityName;
                self.numberofcityid = self.citymessage[row][@"cityid"];
            }else{
                [Tools showAlertWithMessage:@"无法链接网络"];
            }
            break;
        default:
            break;
    }
}

#pragma mark - clickBtn

- (void)respondsTosure:(UIButton *)sender{
    NSLog(@"点击了确定按钮");
    NSLog(@"%@",self.choisCityLable.text);
    if ([_choisCityLable.text isEqualToString:@"无法定位当前位置手动选择"]) {
        [Tools showAlertWithMessage:@"没有选择城市"];
        return;
    }
    [UIView animateWithDuration:1 animations:^{
        self.choisCityLable.frame = CGRectMake(Size_W(-414), Size_H(400) , Size_W(414), Size_H(80));
    } completion:^(BOOL finished) {
            self.choisCityLable.hidden = YES;
            [self.choisCityLable removeFromSuperview];
                [self.navigationController popViewControllerAnimated:YES];
        if (_animationBlock) {
            _animationBlock(_choisCityLable.text,_numberofcityid);
        }
    }];
    
    
}

-(void)respondsTocacel:(UIButton *)sender{
    NSLog(@"点击了取消按钮");
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - GetAndSet

-(UILabel_Font *)currentCityNameLable{
    if (!_currentCityNameLable) {
        _currentCityNameLable = [[UILabel_Font alloc]initWithFrame:CGRectMake(Size_W(0), Size_H(64),Size_W(414), Size_H(44))];
        _currentCityNameLable.font = [UIFont systemFontOfSize:Font(24)];
        _currentCityNameLable.textAlignment = NSTextAlignmentCenter;
        _currentCityNameLable.backgroundColor = ColorWith(232, 232, 232);
        if (self.theCityName) {
            _currentCityNameLable.text = [NSString stringWithFormat:@"当前选择城市为%@",self.theCityName];
        }else{
             _currentCityNameLable.text = @"无法定位您当前位置请手动选择";
        }
    }
    return _currentCityNameLable;
}

- (UIPickerView *)choisCityPickerView{
    if (!_choisCityPickerView) {
        _choisCityPickerView = [[UIPickerView alloc]init];
        _choisCityPickerView.center = CGPointMake(Size_W(207), Size_H(250));
        _choisCityPickerView.delegate = self;
        _choisCityPickerView.dataSource = self;
        _choisCityPickerView.backgroundColor = [UIColor grayColor];
    }
    return _choisCityPickerView;
}



-(UILabel_Font *)choisCityLable{
    if (!_choisCityLable) {
        _choisCityLable = [[UILabel_Font alloc]initWithFrame:CGRectMake(Size_W(0), Size_H(400),Size_W(414), Size_H(80))];
        _choisCityLable.backgroundColor = [UIColor orangeColor];
        NSString *str = [[NSUserDefaults standardUserDefaults] objectForKey:@"cityname"];
        if (str == nil) {
            _choisCityLable.text = @"无法定位当前位置手动选择";
        }else{
        _choisCityLable.text = str;
        }
        _choisCityLable.textAlignment = NSTextAlignmentCenter;
        _choisCityLable.font = [UIFont systemFontOfSize:Font(30)];
    }
    return _choisCityLable;
}

- (UIButton *)sureBtn{
    if (!_sureBtn) {
        _sureBtn = [[UIButton alloc]initWithFrame:CGRectMake(Size_W(50), Size_H(500),Size_W(100), Size_H(44))];
        _sureBtn.backgroundColor = ColorWith(230, 54, 37);
        [_sureBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_sureBtn addTarget:self action:@selector(respondsTosure:) forControlEvents:UIControlEventTouchUpInside];
        _sureBtn.layer.cornerRadius = 10;
    }
    return _sureBtn;
}

- (UIButton *)cacelBtn{
    if (!_cacelBtn) {
        _cacelBtn = [[UIButton alloc]initWithFrame:CGRectMake(Size_W(250), Size_H(500),Size_W(100), Size_H(44))];
        _cacelBtn.backgroundColor = ColorWith(230, 54, 37);
        [_cacelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cacelBtn addTarget:self action:@selector(respondsTocacel:) forControlEvents:UIControlEventTouchUpInside];
        _cacelBtn.layer.cornerRadius = 10;
    }
    return _cacelBtn;
}


@end
